﻿namespace MyTRCP.Common.MyProtocol
{
    public class ClientHelloPayload : BitWalkableObj//client->bridge
    {
        public IdAuthCryptoTypeEnum IdAuthCryptoType { get; set; }
        public ushort ClientIdAuthPublicKeyXLength { get; set; }
        public byte[] ClientIdAuthPublicKeyX { get; set; } = [];
        public ushort ClientIdAuthPublicKeyYLength { get; set; }
        public byte[] ClientIdAuthPublicKeyY { get; set; } = [];
        public HashAlgorithmTypeEnum HashAlgorithmType { get; set; }
        public KeyAgreementAlgorithmTypeEnum KeyAgreementAlgorithmType { get; set; }
        public ushort ClientKeyAgreementPublicKeyXLength { get; set; }
        public byte[] ClientKeyAgreementPublicKeyX { get; set; } = [];
        public ushort ClientKeyAgreementPublicKeyYLength { get; set; }
        public byte[] ClientKeyAgreementPublicKeyY { get; set; } = [];
        public ClientHelloPayload() { }
        public ClientHelloPayload(byte[] data) : base(data) { }
        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            IdAuthCryptoType = (IdAuthCryptoTypeEnum)bw.ReadBits(8);
            ClientIdAuthPublicKeyXLength = (ushort)bw.ReadBits(16);
            ClientIdAuthPublicKeyX = bw.ReadBytes(ClientIdAuthPublicKeyXLength);
            ClientIdAuthPublicKeyYLength = (ushort)bw.ReadBits(16);
            ClientIdAuthPublicKeyY = bw.ReadBytes(ClientIdAuthPublicKeyYLength);
            HashAlgorithmType = (HashAlgorithmTypeEnum)bw.ReadBits(8);
            KeyAgreementAlgorithmType = (KeyAgreementAlgorithmTypeEnum)bw.ReadBits(8);
            ClientKeyAgreementPublicKeyXLength = (ushort)bw.ReadBits(16);
            ClientKeyAgreementPublicKeyX = bw.ReadBytes(ClientKeyAgreementPublicKeyXLength);
            ClientKeyAgreementPublicKeyYLength = (ushort)bw.ReadBits(16);
            ClientKeyAgreementPublicKeyY = bw.ReadBytes(ClientKeyAgreementPublicKeyYLength);
            return bw;
        }

        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            return bw
                .WriteBits((byte)IdAuthCryptoType, 8)
                .WriteBits(ClientIdAuthPublicKeyXLength, 16)
                .WriteBytes(ClientIdAuthPublicKeyX)
                .WriteBits(ClientIdAuthPublicKeyYLength, 16)
                .WriteBytes(ClientIdAuthPublicKeyY)
                .WriteBits((byte)HashAlgorithmType, 8)
                .WriteBits((byte)KeyAgreementAlgorithmType, 8)
                .WriteBits(ClientKeyAgreementPublicKeyXLength, 16)
                .WriteBytes(ClientKeyAgreementPublicKeyX)
                .WriteBits(ClientKeyAgreementPublicKeyYLength, 16)
                .WriteBytes(ClientKeyAgreementPublicKeyY);
        }
    }
}